package io.nio;

import framework.AbstractTest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * Created by shane on 2018/5/20.
 *
 * nio的目标就是快速移动大量数据， 因此ByteBuffer的大小就显得尤为重要
 */
public class ChannelTransfer extends AbstractTest {
    protected static final String TAG = ChannelTransfer.class.getName();
    
    @Override
    public void test(String[] args) {
        super.test(args);

        String inPath = getDefInputFile(args);
        String outPath = getDefOutputFile(args);

        try {
            FileChannel in = new FileInputStream(inPath).getChannel();
            FileChannel out = new FileOutputStream(outPath).getChannel();

            // in.transferTo(0, in.size(), out);
            // or
            out.position(out.size());
            out.transferFrom(in, 0, in.size());

            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String getTag() {
        return TAG;
    }

}
